<!--设置模块唯一ID-->
<div id="system_quanxianguanli_permission_add">
<!--标题、面包屑-->
<section class="content-header">
    <h1>
        权限管理
    </h1>
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
        <li>系统管理</li>
        <li>权限管理</li>
        <li>岗位权限</li>
        <li class="active">添加岗位权限</li>
    </ol>
</section>
<!--列表-->
<section class="content">

</section>
<!--添加、修改弹出层-->
<script type="text/template" data-for="tp_editor">
    <form role="form" class="form-horizontal">
        <div class="u-box">
            <div class="u-box-header">
                <h3>基本信息</h3>
            </div>
            <div class="u-area">
                <div class="form-group">
                    <label class="col-sm-2 control-label">叶子菜单</label>
                    <div class="col-sm-4">
						<input type="hidden" name="menuId" value="<%=data.menuId?data.menuId:''%>">
                        <input type="text" class="form-control" name="menuName" data-complete="menuName" value="<%=data.menuName?data.menuName:''%>">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label"></label>
                    <div class="col-sm-10" >
                        <div style="height: 300px;border:1px #ccc solid;" data-buildByAutocomplete>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="box-footer">
            <button type="submit" class="btn btn-primary pull-right">确认</button>
        </div>
    </form>
</script>

<script type="text/template" data-for="tp_editor1">
    <%for(var i in data){%>
        <div class="col-sm-2 checkbox ">
            <label>
                <input type="checkbox" value="<%=data[i]%>" name="anyName">
                <%=i%>
            </label>
        </div>
    <%}%>
</script>

<script>
	USEN(function (param) {
        //模块ID(全系统唯一)
        var tableId = "#system_quanxianguanli_permission";
        var id = "#system_quanxianguanli_permission_add";
        //定义页面变量
        var $el = $(id);
        var $editor = $el.children('script[data-for=tp_editor]');
        var $editor1 = $el.children('script[data-for=tp_editor1]');
        var that = this;

        var buildAutocompleteEl = function(data){
            $el.find('[data-buildByAutocomplete]').html(function(){
                return _.template($editor1.html())({
                    data:data
                })
            })
        };

        $.ajax({
//            url:"/system/rolesMenu/beforeAdd",
            url:"./simdata/common_add_action.json",
            success:function(data){
                $el.children(".content").html(function(){
                    return _.template($editor.html())({
                        data:data
                    })
                });
                
                that.autocomplete({
                    $el:$el.find('[data-complete=menuName]'),
//                    url:"/system/menu/autoSearchLeafMenuByName",
                    url:"./simdata/common_autocomplete_action.json",
                    param:{
                    	//anykey:"anyValue"
                    },
                    formatItem:function(item){
                        //显示结果
//                        return item["menuName"];
                        return item["name"];
                    },
                    setValue:function(item){
                        //选中结果
//                        return {'data-value':item["menuName"], 'real-value':item["id"]}
                        return {'data-value':item["name"],'real-value':item["id"]};
                    },
                   	choose:function(data){
                   		$el.find("[name=menuId]").val(data["real-value"]);
             		    $el.find("[name=menuName]").val(data["data-value"]);
             		    //拼接多选框字符传，类似字符串拼接和模仿此方法
                        buildAutocompleteEl(data);
                    }
                });
                
                //提交表单
                $el.find("form").ajaxForm({
                	type:"post",
                    url: '/system/rolesMenu/addRolesMenu?roleId=' + param.id,
                    beforeSubmit: function (data) {
                        //console.log("数据提交之前.")
                    },
                    success: function (data, state, obj) {
                        USEN.notifyAlert({
                            title: "提示:",
                            content: '操作成功,刷新列表'
                        });
                        USEN.getDatatable(tableId + ' table.table').ajax.reload();
                        USEN.closeActiveTab();
                    }
                });

            }
        })
    });
</script>
</div>